package com.suneast.suneaststore.modules.personal.wallet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.suneast.suneaststore.API;
import com.suneast.suneaststore.app.AccountManager;
import com.suneast.suneaststore.bean.personal.bank.BankCardBean;
import com.suneast.suneaststore.bean.personal.wallet.WeChatPayBean;
import com.suneast.suneaststore.modules.common.CommonUtil;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.modules.personal.order.event.WithdrawResult;
import com.suneast.suneaststore.modules.personal.wallet.bankcard.event.GotBankCardList;
import com.suneast.suneaststore.modules.personal.wallet.event.GotWeChatTopUpData;
import com.suneast.suneaststore.modules.shopcart.event.BalancePayResult;
import com.suneast.suneaststore.modules.shopcart.event.GotWeChatPayData;
import com.suneast.suneaststore.util.log.LatteLogger;
import com.suneast.suneaststore.util.net.RestClient;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class WalletHandler {
    public static void rechargeByWechatPay(double value) {
        RestClient.builder()
                .url(API.WECHAT_RECHARGE)
                .params("token", AccountManager.getToken())
                .params("num", value)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject dataObj = CommonUtil.parseDataFromRootJSON(response);
                    if (dataObj != null) {
                        JSONObject infoObj = dataObj.getJSONObject("payinfo");
                        WeChatPayBean bean = new WeChatPayBean(
                                dataObj.getString("ordersn"),
                                infoObj.getString("appid"),
                                infoObj.getString("noncestr"),
                                infoObj.getString("package"),
                                infoObj.getString("partnerid"),
                                infoObj.getString("prepayid"),
                                infoObj.getString("timestamp"),
                                infoObj.getString("sign"));
                        EventBus.getDefault().post(new GotWeChatTopUpData(Flag.Result.SUCCEED, bean));
                    } else {
                        EventBus.getDefault().post(new GotWeChatTopUpData(Flag.Result.FAILURE,
                                null));
                    }
                })
                .error((code, msg) -> {
                    EventBus.getDefault().post(new GotWeChatTopUpData(Flag.Result.FAILURE,
                            null));
                    LatteLogger.e("error", " rechargeByWechatPay error code = " + code + " msg = " + msg);
                })
                .failure(() -> {
                    EventBus.getDefault().post(new GotWeChatTopUpData(Flag.Result.FAILURE,
                            null));
                    LatteLogger.e("error", " rechargeByWechatPay failure ");
                })
                .build()
                .post();
    }

    public static void requestWeChatPay(int orderId) {
        RestClient.builder()
                .url(API.WECHAT_PAY)
                .params("token", AccountManager.getToken())
                .params("orderid", orderId)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject dataObj = CommonUtil.parseDataFromRootJSON(response);
                    if (dataObj != null) {
                        JSONObject infoObj = dataObj.getJSONObject("payinfo");
                        WeChatPayBean bean = new WeChatPayBean(
                                dataObj.getString("ordersn"),
                                infoObj.getString("appid"),
                                infoObj.getString("noncestr"),
                                infoObj.getString("package"),
                                infoObj.getString("partnerid"),
                                infoObj.getString("prepayid"),
                                infoObj.getString("timestamp"),
                                infoObj.getString("sign"));
                        EventBus.getDefault().post(new GotWeChatPayData(Flag.Result.SUCCEED, bean));
                    } else {
                        EventBus.getDefault().post(new GotWeChatPayData(Flag.Result.FAILURE,
                                null));
                    }
                })
                .error((code, msg) -> {
                    EventBus.getDefault().post(new GotWeChatPayData(Flag.Result.FAILURE,
                            null));
                    LatteLogger.e("error", " requestWeChatPay error code = " + code + " msg = " + msg);
                })
                .failure(() -> {
                    EventBus.getDefault().post(new GotWeChatPayData(Flag.Result.FAILURE,
                            null));
                    LatteLogger.e("error", " requestWeChatPay failure ");
                })
                .build()
                .post();
    }

    public static void requestBackCardList() {
        RestClient.builder()
                .url(API.BANK_CARD_LIST)
                .params("token", AccountManager.getToken())
                .success(response -> {
                    JSONObject rootObj = JSONObject.parseObject(response);
                    LatteLogger.json("test", response);
                    if (rootObj.getIntValue("code") == 1) {
                        JSONArray dataArray = rootObj.getJSONArray("data");
                        if (dataArray != null && dataArray.size() > 0) {
                            ArrayList<BankCardBean> bankCardList = new ArrayList<>();
                            JSONObject cardObj;
                            for (int i = 0, iLength = dataArray.size(); i < iLength; i++) {
                                cardObj = dataArray.getJSONObject(i);
                                bankCardList.add(new BankCardBean(
                                        cardObj.getIntValue("id"),
                                        cardObj.getString("bankname"),
                                        cardObj.getString("num"),
                                        cardObj.getString("picture"),
                                        cardObj.getIntValue("banktype"),
                                        cardObj.getString("color")));
                            }
                            EventBus.getDefault().post(new GotBankCardList(bankCardList));
                        }
                    }
                })
                .failure(() -> {
                    LatteLogger.e("test", "requestBackCardList failure ");
                })
                .error((code, msg) -> {
                    LatteLogger.e("test", "requestBackCardList error code = "
                            + code + " msg = " + msg);
                })
                .build()
                .post();
    }

    public static void requestWithdraw(int type, double moneyValue, int bankId) {
        RestClient.builder()
                .url(API.WITHDRAW)
                .params("token", AccountManager.getToken())
                .params("type", type)
                .params("money", moneyValue)
                .params("bankId", bankId)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject dataObj = CommonUtil.parseDataFromRootJSON(response);
                    if (dataObj != null) {
                        EventBus.getDefault().post(new WithdrawResult(Flag.Result.SUCCEED,
                                dataObj.getDoubleValue("balance")));
                    } else {
                        EventBus.getDefault().post(new WithdrawResult(Flag.Result.FAILURE, "数据错误"));
                    }
                })
                .error((code, msg) -> {
                    EventBus.getDefault().post(new WithdrawResult(Flag.Result.FAILURE, msg));
                    LatteLogger.e("error", " requestWeChatPay error code = " + code + " msg = " + msg);
                })
                .failure(() -> {
                    EventBus.getDefault().post(new WithdrawResult(Flag.Result.FAILURE, "数据错误"));
                    LatteLogger.e("error", " requestWeChatPay failure ");
                })
                .build()
                .post();
    }

    public static void requestBalancePay(int orderId, String pass) {
        RestClient.builder()
                .url(API.BALANCE_PAY)
                .params("token", AccountManager.getToken())
                .params("orderid", orderId)
                .params("pass", pass)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject dataObj = CommonUtil.parseDataFromRootJSON(response);
                    if (dataObj != null) {
                        EventBus.getDefault().post(new BalancePayResult(Flag.Result.SUCCEED,
                                dataObj.getDoubleValue("credit1"), dataObj.getDoubleValue("credit2")));
                    } else {
                        EventBus.getDefault().post(new BalancePayResult(Flag.Result.FAILURE, "数据错误"));
                    }
                })
                .error((code, msg) -> {
                    EventBus.getDefault().post(new BalancePayResult(Flag.Result.FAILURE, msg));
                    LatteLogger.e("error", " requestBalancePay error code = " + code + " msg = " + msg);
                })
                .failure(() -> {
                    EventBus.getDefault().post(new BalancePayResult(Flag.Result.FAILURE, "数据错误"));
                    LatteLogger.e("error", " requestBalancePay failure ");
                })
                .build()
                .post();
    }
}
